package org.dalmatian.app.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dalmatian.common.core.domain.R;
import org.dalmatian.common.excel.utils.ExcelUtil;
import org.dalmatian.common.mybatis.core.page.PageQuery;
import org.dalmatian.common.mybatis.core.page.TableDataInfo;
import org.dalmatian.common.web.core.BaseController;
import org.dalmatian.app.system.domain.bo.SysDictTypeBo;
import org.dalmatian.app.system.domain.vo.SysDictTypeVo;
import org.dalmatian.app.system.service.ISysDictTypeService;
import jakarta.servlet.http.HttpServletResponse;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.noear.solon.annotation.Body;
import org.noear.solon.annotation.Controller;
import org.noear.solon.annotation.Delete;
import org.noear.solon.annotation.Get;
import org.noear.solon.annotation.Mapping;
import org.noear.solon.annotation.Path;
import org.noear.solon.annotation.Post;
import org.noear.solon.annotation.Put;
import org.noear.solon.core.handle.MethodType;
import org.noear.solon.validation.annotation.Validated;

/**
 * 数据字典信息
 *
 * @author Lion Li
 */
@RequiredArgsConstructor
@Controller
@Mapping("/system/dict/type")
public class SysDictTypeController extends BaseController {

    private final ISysDictTypeService dictTypeService;

    /**
     * 查询字典类型列表
     */
    @SaCheckPermission("system:dict:list")
    @Get
    @Mapping("/list")
    public TableDataInfo<SysDictTypeVo> list(SysDictTypeBo dictType, PageQuery pageQuery) {
        return dictTypeService.selectPageDictTypeList(dictType, pageQuery);
    }

    /**
     * 导出字典类型列表
     */
    //@Log(title = "字典类型", businessType = BusinessType.EXPORT)
    @SaCheckPermission("system:dict:export")
    @Post
    @Mapping("/export")
    public void export(SysDictTypeBo dictType, HttpServletResponse response) {
        List<SysDictTypeVo> list = dictTypeService.selectDictTypeList(dictType);
        ExcelUtil.exportExcel(list, "字典类型", SysDictTypeVo.class);
    }

    /**
     * 查询字典类型详细
     *
     * @param dictId 字典ID
     */
    @SaCheckPermission("system:dict:query")
    @Mapping(method = MethodType.GET, value = "/{dictId}")
    public R<SysDictTypeVo> getInfo(@Path("dictId") Long dictId) {
        return R.ok(dictTypeService.selectDictTypeById(dictId));
    }

    /**
     * 新增字典类型
     */
    @SaCheckPermission("system:dict:add")
    //@Log(title = "字典类型", businessType = BusinessType.INSERT)
    @Post
    @Mapping
    public R<Void> add(@Validated @Body SysDictTypeBo dict) {
        if (!dictTypeService.checkDictTypeUnique(dict)) {
            return R.fail("新增字典'" + dict.getDictName() + "'失败，字典类型已存在");
        }
        dictTypeService.insertDictType(dict);
        return R.ok();
    }

    /**
     * 修改字典类型
     */
    @SaCheckPermission("system:dict:edit")
    //@Log(title = "字典类型", businessType = BusinessType.UPDATE)
    @Put
    @Mapping
    public R<Void> edit(@Validated @Body SysDictTypeBo dict) {
        if (!dictTypeService.checkDictTypeUnique(dict)) {
            return R.fail("修改字典'" + dict.getDictName() + "'失败，字典类型已存在");
        }
        dictTypeService.updateDictType(dict);
        return R.ok();
    }

    /**
     * 删除字典类型
     *
     * @param dictIds 字典ID串
     */
    @SaCheckPermission("system:dict:remove")
    //@Log(title = "字典类型", businessType = BusinessType.DELETE)
    @Mapping(method = MethodType.DELETE, value = "/{dictIds}")
    public R<Void> remove(@Path("dictIds") Long[] dictIds) {
        dictTypeService.deleteDictTypeByIds(dictIds);
        return R.ok();
    }

    /**
     * 刷新字典缓存
     */
    @SaCheckPermission("system:dict:remove")
    //@Log(title = "字典类型", businessType = BusinessType.CLEAN)
    @Delete
    @Mapping("/refreshCache")
    public R<Void> refreshCache() {
        dictTypeService.resetDictCache();
        return R.ok();
    }

    /**
     * 获取字典选择框列表
     */
    @Get
    @Mapping("/optionselect")
    public R<List<SysDictTypeVo>> optionselect() {
        List<SysDictTypeVo> dictTypes = dictTypeService.selectDictTypeAll();
        return R.ok(dictTypes);
    }
}
